package uz.rt.dicom.data.element;

import uz.rt.dicom.Padding;
import uz.rt.dicom.ValueRepresentation;
import uz.rt.dicom.data.ComparableList;
import uz.rt.dicom.data.StringElement;
import uz.rt.dicom.io.DicomInputStream;
import uz.rt.dicom.tag.DicomTag;

import java.io.IOException;

public class UnlimitedText extends StringElement implements Comparable<UnlimitedText>
{
// --------------------------- CONSTRUCTORS ---------------------------

    public UnlimitedText(DicomTag tag)
    {
        super(tag);
        setPadding(Padding.TRAILING_SPACE);
    }

// ------------------------ INTERFACE METHODS ------------------------


// --------------------- Interface Comparable ---------------------


    public int compareTo(UnlimitedText o)
    {
        if (!tag.equals(o.getTag())) return tag.compareTo(o.getTag());
        if (!vm.equals(o.getVm())) return vm.compareTo(o.getVm());
        return value.compareTo(o.getValue());
    }

// --------------------- Interface IDataElement ---------------------

    @Override
    public byte[] getVR()
    {
        return ValueRepresentation.UT;
    }

    @Override
    public ValueRepresentation getValueRepresentation()
    {
        return ValueRepresentation.UnlimitedText;
    }

    public void read(DicomInputStream dicomInputStream, int length) throws IOException
    {
        value = null;
        value = new ComparableList<String>();
        String s = dicomInputStream.readString(length, getCharacterSet());

        while (s.endsWith(" ") && s.length() > 0)
        {
            s = s.substring(0, s.length() - 1);
        }
        value.add(s);
    }
}
